//
//  SISSSearchViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/3/31.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSSearchViewController.h"

#import "SISSNetworkingManager+Home.h"
#import "SISSFindHouseResourcesViewController.h"
#import "SISSFindPassengerResourceViewController.h"
#import "SISSFindRentOutViewController.h"
#import "SISSFindTenantViewController.h"
#import "SISSFindCooperationViewController.h"
#import "SISSFindPassengerCooperationViewController.h"
#import "SISSNavigationController.h"

@interface SISSSearchViewController ()<PYSearchViewControllerDelegate>

/** searchViewController */
@property (weak, nonatomic) PYSearchViewController *searchViewController;

@end

@implementation SISSSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self addSearchController];
    
}


- (void)addSearchController {
    
    // 1. 创建热门搜索
    //    NSArray *hotSeaches = @[];
    
    // 2. 创建控制器
    PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:@[] searchBarPlaceholder:@"请输入区域、街道办或小区" didSearchBlock:^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {
        
        // 跳转控制器
        switch (self.searchType) {
            case SISSSearchTypeHouse: {
                
                SISSFindHouseResourcesViewController *vc = [[SISSFindHouseResourcesViewController alloc] init];
                vc.title = @"房源";
                vc.keyword = searchText;
                [searchViewController.navigationController pushViewController:vc animated:YES];
            }
                break;
                
            case SISSSearchTypePassenger: {
                
                SISSFindPassengerResourceViewController *vc = [[SISSFindPassengerResourceViewController alloc] init];
                vc.title = @"客源";
                vc.keyword = searchText;
                [searchViewController.navigationController pushViewController:vc animated:YES];
            }
                break;
                
            case SISSSearchTypeRentOut: {
                
                SISSFindRentOutViewController *vc = [[SISSFindRentOutViewController alloc] init];
                vc.title = @"出租";
                vc.keyword = searchText;
                [searchViewController.navigationController pushViewController:vc animated:YES];
            }
                break;
                
            case SISSSearchTypeTenant: {
                
                SISSFindTenantViewController *vc = [[SISSFindTenantViewController alloc] init];
                vc.title = @"租客";
                vc.keyword = searchText;
                [searchViewController.navigationController pushViewController:vc animated:YES];
            }
                break;
                
            case SISSSearchTypeHouseCooperation: {
                
                SISSFindCooperationViewController *vc = [[SISSFindCooperationViewController alloc] init];
                vc.title = @"房源合作";
                vc.keyword = searchText;
                [searchViewController.navigationController pushViewController:vc animated:YES];
            }
                break;
                
            case SISSSearchTypePassengerCooperation: {
                
                SISSFindPassengerCooperationViewController *vc = [[SISSFindPassengerCooperationViewController alloc] init];
                vc.title = @"客源合作";
                vc.keyword = searchText;
                [searchViewController.navigationController pushViewController:vc animated:YES];
            }
                break;
                
            default:
                break;
        }
        
    }];
    
//    self.searchViewController = searchViewController;
    searchViewController.searchType = self.searchType;
    searchViewController.searchHistoriesCount = 3; // 搜索历史
    
    // 3. 设置风格
    searchViewController.searchHistoryStyle = PYSearchHistoryStyleDefault; // 搜索历史风格为default
    
    // 4. 设置代理
    searchViewController.delegate = self;
    
    // 5. 跳转到搜索控制器
    SISSNavigationController *nav = [[SISSNavigationController alloc] initWithRootViewController:searchViewController];
    //        [self presentViewController:nav animated:NO completion:nil];
    
    [self addChildViewController:nav];
    [self.view addSubview:nav.view];
    nav.view.frame = self.view.bounds;
    
}

#pragma mark - PYSearchViewControllerDelegate

- (void)chooseLeftViewWithTitle:(NSString *)title selectIndex:(NSInteger)index {
    
    switch (index) {
        case 0:
            self.searchType = SISSSearchTypeHouse;
            break;
            
        case 1:
            self.searchType = SISSSearchTypePassenger;
            break;
            
        case 2:
            self.searchType = SISSSearchTypeRentOut;
            break;
            
        case 3:
            self.searchType = SISSSearchTypeTenant;
            break;
            
        case 4:
            self.searchType = SISSSearchTypeHouseCooperation;
            break;
            
        case 5:
            self.searchType = SISSSearchTypePassengerCooperation;
            break;
            
        default:
            break;
    }
}

- (void)searchViewController:(PYSearchViewController *)searchViewController searchTextDidChange:(UISearchBar *)seachBar searchText:(NSString *)searchText {
    NSString *searchTextStr = [searchText lowercaseString];
    if (searchText.length) { // 与搜索再搜索
        
        switch (self.searchType) {
            case SISSSearchTypeHouse: {
                
                [kNetworkManager requestFuzzySearchHouseWithKey:searchTextStr success:^(NSArray<SISSFuzzySearchModel *> *fuzzySearchResults) {
                    
                    searchViewController.searchSuggestions = fuzzySearchResults;
                    
                    
                } failed:^(NSError *error) {
                    
                    NSLog(@"%@",error);
                }];
            }
                break;
                
            case SISSSearchTypePassenger: {
                
                [kNetworkManager requestFuzzySearchPassengerWithKey:searchTextStr success:^(NSArray<SISSFuzzySearchModel *> *fuzzySearchResults) {
                    
                    searchViewController.searchSuggestions = fuzzySearchResults;
                } failed:^(NSError *error) {
                    
                    NSLog(@"%@",error);
                }];
            }
                break;
                
            case SISSSearchTypeRentOut: {
                
                [kNetworkManager requestFuzzySearchRentOutWithKey:searchTextStr success:^(NSArray<SISSFuzzySearchModel *> *fuzzySearchResults) {
                    
                    searchViewController.searchSuggestions = fuzzySearchResults;
                    
                    
                } failed:^(NSError *error) {
                    
                    NSLog(@"%@",error);
                }];
            }
                break;
                
            case SISSSearchTypeTenant: {
                
                [kNetworkManager requestFuzzySearchTenantWithKey:searchTextStr success:^(NSArray<SISSFuzzySearchModel *> *fuzzySearchResults) {

                    searchViewController.searchSuggestions = fuzzySearchResults;
                } failed:^(NSError *error) {

                    NSLog(@"%@",error);
                }];
            }
                break;
                
            case SISSSearchTypeHouseCooperation: {
                
                [kNetworkManager requestFuzzySearchHouseCooperationWithKey:searchTextStr success:^(NSArray<SISSFuzzySearchModel *> *fuzzySearchResults) {
                    
                    searchViewController.searchSuggestions = fuzzySearchResults;
                } failed:^(NSError *error) {
                    
                    NSLog(@"%@",error);
                }];
            }
                break;
                
            case SISSSearchTypePassengerCooperation: {
                
                [kNetworkManager requestFuzzySearchPassengerCooperationWithKey:searchTextStr success:^(NSArray<SISSFuzzySearchModel *> *fuzzySearchResults) {
                    
                    searchViewController.searchSuggestions = fuzzySearchResults;
                } failed:^(NSError *error) {
                    
                    NSLog(@"%@",error);
                }];
            }
                break;
                
            default:
                break;
        }
        
    }
}


@end
